package models

import "time"

type FormData struct {
	Title        string `form:"title"`
	Description  string `form:"description"`
	Ingredients  string `form:"ingredients"`
	Instructions string `form:"instructions"`
}

type Recipes struct {
	RecipeID     uint64         `gorm:"column:recipe_id;primary_key;auto_increment" json:"recipe_id"`
	Title        string         `gorm:"column:title;not null" json:"title" `
	Description  string         `gorm:"column:description;not null" json:"description"`
	Ingredients  string         `gorm:"column:ingredients;not null" json:"ingredients" `
	Instructions string         `gorm:"column:instructions;not null" json:"instructions" `
	ImageURL     string         `gorm:"column:image_url;not null" json:"image_url"`
	UserID       uint64         `gorm:"column:user_id" json:"user_id"`
	IsBlogPost   int            `gorm:"is_blog_post" json:"is_blog_post"`
	CreatedAt    time.Time      `gorm:"column:create_at;autoCreateTime" json:"create_at"`
	UpdatedAt    time.Time      `gorm:"column:updated;autoUpdateTime" json:"updated_at"`
	Flags        []Tags         `gorm:"many2many:recipes_flag_map"`
	Tools        []CookingTools `gorm:"many2many:recipes_tool_map"`
	User         UserInfo       `gorm:"foreignKey:UserID;"`
}

func (recipes *Recipes) Delete() error {
	return DB.Self.Delete(&recipes).Error
}
func (recipes *Recipes) Create() error {
	return DB.Self.Create(&recipes).Error
}

func (u *Recipes) Update() error {
	return DB.Self.Model(&u).Update(&u).Error
}

// 查询，提供Id查询
func (rec *Recipes) GetRecipesById() error {
	return DB.Self.Preload("User").First(&rec).Error
}

// 查询个人食谱
func GetRecipeByUserId(userId uint64) ([]*Recipes, error) {
	recipes := make([]*Recipes, 0)
	err := DB.Self.Where("user_id = ?", userId).Find(&recipes).Error
	return recipes, err
}

// 查询所有
func GetRecipe() ([]*Recipes, error) {
	recipes := make([]*Recipes, 0)
	err := DB.Self.Find(&recipes).Error
	return recipes, err
}

// 仿写模糊查询
func GetRecipeByLike(key string) ([]*Recipes, error) {
	recipeList := make([]*Recipes, 0)
	err := DB.Self.Where("title like ? ", "%"+key+"%").Find(&recipeList).Error
	return recipeList, err
}

func GetRecipeLimit() ([]*Recipes, error) {
	recipes := make([]*Recipes, 0)
	err := DB.Self.Limit(10).Find(&recipes).Error
	return recipes, err
}
